//
//  ZFMyBaseInfoViewController.m
//  HappilyLoan
//
//  Created by zainishenbian on 2017/3/29.
//  Copyright © 2017年 zongqingxiangqian. All rights reserved.
//

#import "ZFMyBaseInfoViewController.h"
#import "ZFAuthorizeInfoViewController.h"//信息授权VC
#import "ZFContactViewController.h"//紧急联系人VC
#import "ZFFinanceInfoViewController.h"//财务信息VC
#import "ZFProfessionInfoViewController.h"//职业信息VC
#import "ValuePickerView.h"
#import "ZFHeader.h"

@interface ZFMyBaseInfoViewController ()
{
    NSString *startTime;//进入页面的时间 
    
    NSArray *shenfenArray;//社会身份数组
    NSArray *eduArray;//教育程度数组
    NSArray *marrayStateArray;//婚姻状况
    NSArray *houseArray;//住房情况有无
    NSArray *creditArray;//信用卡（）
    
    NSArray *certainlyWriteUIArray;//必填选项UI数组
}

@property (nonatomic,strong) ValuePickerView *pickerView;
@property (nonatomic,strong) ZFMyBasicInfoModal *basicData;
@property (nonatomic,strong) AlertBoxView *alertV;

@end

@implementation ZFMyBaseInfoViewController

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [MobClick beginLogPageView:@"myBaseInfo"];
    self.navigationController.navigationBarHidden = NO;
}

- (void)viewDidLoad {
    
    [super viewDidLoad];
    self.title = @"社会信息";
    _alertV = [[AlertBoxView alloc] init];
    [self.view addSubview:_alertV];
    [self initData];
}
- (void)initData
{
    startTime = [ZFPulicAPI getCurrentTimestamp];
    
    [ZFMyInfoHttpRequest requestfindUserBasicInfosucessBlock:^(NSDictionary *dict) {
        
        if ([dict[@"code"] integerValue] == 1) {
            
            if (dict[@"data"] == nil) {
                
                
            }else{
                ZFMyBasicInfoModal *modal = [ZFMyBasicInfoModal getMyBasicInfoDictionary:dict[@"data"]];
                self.basicData = modal;
                [self showData];
                NSLog(@"%@-----%@",modal.socialInfoModal.edu_level,modal.financelInfoModal.salary_income);
            }
        }

    } failureBlock:^(NSInteger failSign) {
        [_alertV appearAndDisappearAlertWithString:@"网络连接失败"];
    }];
    certainlyWriteUIArray = @[_userTypeButton,_eduButton,_incomeButton,_contactButton,_chooseCard];
    //初始化数据
    _pickerView = [[ValuePickerView alloc] init];
    
    shenfenArray = @[@"在职人员",@"企业负责人",@"自由职业",@"无业",@"退休",@"个体工商户",@"个体经营者",@"学生"];
    eduArray = @[@"初中以下",@"初中",@"高中/中专/技校",@"专科",@"本科",@"硕士",@"博士及以上"];
    marrayStateArray = @[@"未婚",@"已婚",@"离异/丧偶"];
    houseArray = @[@"自置无按揭",@"自置有按揭",@"亲属楼宇",@"集体宿舍",@"租房",@"自建房",@"共有",@"其他"];
    creditArray = @[@"有信用卡",@"无信用卡"];
}
#pragma mark ———— show data
- (void)showData
{
    [_userTypeButton setTitle:shenfenArray[[_basicData.socialInfoModal.social_identity integerValue]-1] forState:UIControlStateNormal];
    [_eduButton setTitle:eduArray[[_basicData.socialInfoModal.edu_level integerValue]-1] forState:UIControlStateNormal];
    [_chooseCard setTitle:creditArray[[_basicData.socialInfoModal.own_credit integerValue]-1] forState:UIControlStateNormal];
    
    [_incomeButton setTitle:@"已填写" forState:UIControlStateNormal];
    [_contactButton setTitle:@"已填写" forState:UIControlStateNormal];
    if (_basicData.professionInfoModal) {
        
        [_professionButton setTitle:@"已填写" forState:UIControlStateNormal];
    }
    if ([_basicData.socialInfoModal.marry_state integerValue] != 0) {
        
        [_marriageButton setTitle:marrayStateArray[[_basicData.socialInfoModal.marry_state integerValue]-1] forState:UIControlStateNormal];
    }
    if ([_basicData.socialInfoModal.house_state integerValue] != 0) {
        
        [_houseButton setTitle:houseArray[[_basicData.socialInfoModal.house_state integerValue]-1] forState:UIControlStateNormal];
    }
}
#pragma mark ———— button的点击事件
- (IBAction)chooseClick:(id)sender {
    
    UIButton *button = (UIButton *)sender;
    switch (button.tag) {
        case 1:
        {
            _pickerView.dataSource = shenfenArray;
            _pickerView.pickerTitle = @"社会身份";
            self.pickerView.valueDidSelect = ^(NSString *value) {
                
                NSArray * stateArr = [value componentsSeparatedByString:@"/"];
                [button setTitle:stateArr[0] forState:UIControlStateNormal];
            };
            [_pickerView show];
        }
            break;
            
        case 2:
        {
            _pickerView.dataSource = eduArray;
            _pickerView.pickerTitle = @"教育程度";
            self.pickerView.valueDidSelect = ^(NSString *value) {
                
                NSArray * stateArr = [value componentsSeparatedByString:@"/"];
                [button setTitle:stateArr[0] forState:UIControlStateNormal];
            };
            [_pickerView show];
        }
            break;
            
        case 3:
        {
            ZFFinanceInfoViewController *financeInfoVC = [ZFFinanceInfoViewController new];
            financeInfoVC.submitStatuBlock = ^(NSInteger submitStatu) {
                
                if (submitStatu == 1) {
                    
                    [button setTitle:@"已填写" forState:UIControlStateNormal];
                    [button setTitleColor:Main_Color1 forState:UIControlStateNormal];
                }
            };
            financeInfoVC.financeModal = _basicData.financelInfoModal;
            [self.navigationController pushViewController:financeInfoVC animated:YES];
        }
            break;
            
        case 4:
            
        {
            ZFContactViewController *contactVC = [ZFContactViewController new];
            contactVC.submitStatuBlock = ^(NSInteger submitStatu) {
                
                if (submitStatu == 1) {
                    
                    [button setTitle:@"已填写" forState:UIControlStateNormal];
                    [button setTitleColor:Main_Color1 forState:UIControlStateNormal];
                }
            };
            contactVC.contactArray = _basicData.contactArray;
            [self.navigationController pushViewController:contactVC animated:YES];
        }
            break;
        case 5:
        {
            _pickerView.dataSource = creditArray;
            _pickerView.pickerTitle = @"信用卡";
            self.pickerView.valueDidSelect = ^(NSString *value) {
                
                NSArray * stateArr = [value componentsSeparatedByString:@"/"];
                [button setTitle:stateArr[0] forState:UIControlStateNormal];
            };
            [_pickerView show];
        }
            break;
            
        case 6:
        {
            ZFProfessionInfoViewController *professionInfoVC = [ZFProfessionInfoViewController new];
            
            professionInfoVC.submitStatuBlock = ^(NSInteger submitStatu) {
                
                if (submitStatu == 1) {
                    
                    [button setTitle:@"已填写" forState:UIControlStateNormal];
                    [button setTitleColor:Main_Color1 forState:UIControlStateNormal];
                }
            };
            professionInfoVC.professionInfoModal = _basicData.professionInfoModal;
            [self.navigationController pushViewController:professionInfoVC animated:YES];
        }
            break;
            
        case 7:
        {
            _pickerView.dataSource = marrayStateArray;
            _pickerView.pickerTitle = @"婚姻状况";
            self.pickerView.valueDidSelect = ^(NSString *value) {
                
                [button setTitle:value forState:UIControlStateNormal];
            };
            [_pickerView show];
            
        }
            break;
            
        case 8:
        {
            _pickerView.dataSource = houseArray;
            _pickerView.pickerTitle = @"住房状况";
            self.pickerView.valueDidSelect = ^(NSString *value) {
                
                NSArray * stateArr = [value componentsSeparatedByString:@"/"];
                [button setTitle:stateArr[0] forState:UIControlStateNormal];
            };
            [_pickerView show];
            
        }
            
            break;
            
        default:
            break;
    }

}
//提交信息
- (IBAction)sureClick:(id)sender {
    
    if ([self judgeSubmitInfo]) {
        
        ZFUserSocialInfomodal *modal = [ZFUserSocialInfomodal new];
        modal.social_identity = [self getSingleSubmit:shenfenArray writeInfo:_userTypeButton.titleLabel.text];
        modal.edu_level = [self getSingleSubmit:eduArray writeInfo:_eduButton.titleLabel.text];
        modal.startTime = startTime;
        modal.own_credit = [self getSingleSubmit:creditArray writeInfo:_chooseCard.titleLabel.text];
        if ([_marriageButton.titleLabel.text rangeOfString:@"请"].location == NSNotFound) {
            
            modal.marry_state = [self getSingleSubmit:marrayStateArray writeInfo:_marriageButton.titleLabel.text];
        }
        else
        {
            modal.marry_state = @"";
        }
        if ([_houseButton.titleLabel.text rangeOfString:@"请"].location == NSNotFound) {
            
            modal.house_state = [self getSingleSubmit:houseArray writeInfo:_houseButton.titleLabel.text];
        }
        else
        {
            modal.house_state = @"";
        }

        [ZFApplyInfoHttpRequest requestSubmitUserSocialInfo:modal sucessBlock:^(NSDictionary *dict) {
            
            if ([dict[@"is_alert"] integerValue] == 1) {
                
                [self showAlertView:dict[@"msg"]];
            }
            if ([dict[@"code"] integerValue] == 1) {
                
                [self.navigationController popViewControllerAnimated:YES];
            }
        } failureBlock:^(NSInteger failSign) {
            
        }];
    }
}
#pragma mark ———— 判断提交信息是否为空
- (BOOL)judgeSubmitInfo
{
    for (UIButton *button in certainlyWriteUIArray) {
        
        if ([button.titleLabel.text rangeOfString:@"请"].location != NSNotFound) {
            return NO;
        }
    }
    return YES;
}

- (NSString *)getSingleSubmit:(NSArray *)array writeInfo:(NSString *)writeStr
{
    for (int i = 0; i < array.count; i ++) {
        
        if ([array[i] isEqualToString:writeStr]) {
            
            NSLog(@"%d",i+1);
            return [NSString stringWithFormat:@"%d",i+1];
        }
    }
    return @"0";
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [MobClick endLogPageView:@"myBaseInfo"];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
